﻿<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="SRM" %>
<%@ Import Namespace="SRM.DataObjects" %>
<%@ Page Title="" Language="C#" MasterPageFile="~/ApplicantSite.master" %>

<script runat="server">

    public DataTable Applicant { get; set; }
    public string ApplicantKey { get; set; }

    public string PreviousApplicant { get; set; }
    public string NextApplicant { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.errorMessage.Text = string.Empty;

        if ((!Request.IsAuthenticated) || (Session["ApplicantKey"] == null))
        {
            if (Request.QueryString.Count > 0)
                Response.Redirect("~/LoginForm.aspx?" + Request.QueryString.ToString());
            else
                Response.Redirect("~/LoginForm.aspx");
        }


        this.hdn_UseSAPCode.Value = "1";
        this.ApplicantKey = Session["ApplicantKey"].ToString();
        this.hdn_applicantKey.Value = Session["ApplicantKey"].ToString();
        int applicantKey = int.Parse(Session["ApplicantKey"].ToString());

        #region Setup ApplicantKey

        if (Request.QueryString["key"] != null)
        {
            if (!int.TryParse(Request.QueryString["key"], out applicantKey))
            {
                applicantKey = int.Parse(Session["ApplicantKey"].ToString());
            }
            else
            {
                Session["ApplicantKey"] = applicantKey;
            }
        }

        #endregion Setup ApplicantKey

        DataTable dtPrevNext = SRM_Applicant.GetPrevAndNextApplicant(applicantKey.ToString());
        if (dtPrevNext.Rows.Count > 0)
        {
            if (dtPrevNext.Rows[0]["ApplicantKey"] == DBNull.Value)
                PreviousApplicant = string.Empty;
            else
                PreviousApplicant = string.Format("<a href=\"ApplicantForm.aspx?key={0}\">prev</a>", dtPrevNext.Rows[0]["ApplicantKey"].ToString());

            if (dtPrevNext.Rows[1]["ApplicantKey"] == DBNull.Value)
                NextApplicant = string.Empty;
            else
                NextApplicant = string.Format("<a href=\"ApplicantForm.aspx?key={0}\">next</a>", dtPrevNext.Rows[1]["ApplicantKey"].ToString());
        }


        #region Setup Position (if any else unsolicited)

        if (((Session["RequisitionKey"] != null) && (Session["RequisitionKey"].ToString() != SRM_Requisition.GetUnsolicitedRequisitionKey())) ||
            (Request.QueryString["pos"] != null))
        {

            string requisitionKey = string.Empty;

            if ((string.IsNullOrEmpty(requisitionKey)) && (Session["RequisitionKey"] != null))
                requisitionKey = Session["RequisitionKey"].ToString().Trim();

            if ((string.IsNullOrEmpty(requisitionKey)) && (Request.QueryString["pos"] != null))
                requisitionKey = Request.QueryString["pos"].Trim();

            Session["RequisitionKey"] = requisitionKey.ToString();

            DataTable dtRequisition = SRM_Requisition.GetRequisition(Session["RequisitionKey"].ToString());

            if ((dtRequisition.Rows.Count > 0) && (Request.QueryString["pos"] != null))
            {
                role_literal.Text = string.Format("<p>You are applying for the role: {0}</p>", dtRequisition.Rows[0]["JobTitleFreeText"].ToString());
            }

            for (int tabIndex = 2; tabIndex < RadTabStrip.Tabs.Count; tabIndex++)
            {
                RadTabStrip.Tabs[tabIndex].Visible = false;
            }

        }
        else
        {
            Session["RequisitionKey"] = SRM_Requisition.GetUnsolicitedRequisitionKey();
        }
        this.hdn_requisitionKey.Value = Session["RequisitionKey"].ToString();

        #endregion Setup Position (if any else unsolicited)

        this.Applicant = SRM_Applicant.GetApplicant(applicantKey);
    }

    private void SaveForm()
    {
        Telerik.Web.UI.RadMultiPage multipage = Help.FindControlRecursive(Page.Controls, "RadMultiPage") as Telerik.Web.UI.RadMultiPage;

        if (multipage == null)
            return;

        foreach (Telerik.Web.UI.RadPageView pageview in multipage.PageViews)
        {
            foreach (Control pvc in pageview.Controls)
            {
                if (pvc is IUserInput)
                {
                    ((IUserInput)pvc).SaveUserInput();
                }
            }
        }

        // Insert into SRM_RequisitionApplicant_Tbl
        SRM_Requisition.InsertApplicant(Session["ApplicantKey"].ToString(), Session["RequisitionKey"].ToString());
    }

    protected void SaveApplicationForm_Button_Click(object sender, EventArgs e)
    {
        this.SaveForm();
    } // protected void SaveApplicationForm_Button_Click

    protected void SubmitApplicationForm_Button_Click(object sender, EventArgs e)
    {
        this.SaveForm();

        // Check if Form is Complete
        DataTable dt = null;
        DataRow dr = null;
        int applicantKey = int.Parse(Session["ApplicantKey"].ToString());
        SRM_Applicant.GetApplicant(applicantKey);

        bool isValid = true;
        bool formIsComplete = true;
        List<string> problemTabs = new List<string>();

        //--We need to check the following tabs:
        //--Personal Particulars
        dt = SRM_Applicant.GetPersonalParticulars(applicantKey.ToString());
        if (dt.Rows.Count > 0)
        {
            isValid = true;
            dr = dt.Rows[0];
            if ((isValid) && (string.IsNullOrEmpty(dr["VacancyMedium"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Salutation"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["GivenName"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["LastName"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["NameFormat"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["NRICNo"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["NRICType"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["NRICIssuingDate"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Citizenship"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["PlaceOfBirth"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["StreetName"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["PostalCode"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["CountryCode"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["City"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["State"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["MobileNo"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["HomeNo"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["DrivingClass"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["YrsExperience"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["PeriodNotice"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Availability"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["AskingSalary"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["PdpaConsent"].ToString())))
                isValid = false;

            if (!isValid)
            {
                problemTabs.Add("Personal Particulars");
                formIsComplete = false;
            }
        }
        //--Resume File
        if (dt.Rows.Count > 0)
        {
            isValid = true;
            dr = dt.Rows[0];

            if ((isValid) && (string.IsNullOrEmpty(dr["ResumeFileName"].ToString())))
                isValid = false;

            if (!isValid)
            {
                problemTabs.Add("Resume");
                formIsComplete = false;
            }
        }
        //--National Service
        if (dt.Rows.Count > 0)
        {
            isValid = true;
            dr = dt.Rows[0];

            if ((isValid) && (string.IsNullOrEmpty(dr["NS"].ToString())))
                isValid = false;
            else
            {
                if (dr["NS"].ToString() != "1")
                {
                    if ((isValid) && (string.IsNullOrEmpty(dr["NSServiceTypeCode"].ToString())))
                        isValid = false;
                    if ((isValid) && (string.IsNullOrEmpty(dr["NSStatus"].ToString())))
                        isValid = false;
                    if ((isValid) && (string.IsNullOrEmpty(dr["NSVocation"].ToString())))
                        isValid = false;
                    if ((isValid) && (string.IsNullOrEmpty(dr["NSServiceRankCode"].ToString())))
                        isValid = false;
                    if ((isValid) && (string.IsNullOrEmpty(dr["NSORDDate"].ToString())))
                        isValid = false;
                }
            }

            if (!isValid)
            {
                problemTabs.Add("National Service");
                formIsComplete = false;
            }
        }
        //--Addition Personal Particulars
        if (dt.Rows.Count > 0)
        {
            isValid = true;
            dr = dt.Rows[0];

            if ((isValid) && (string.IsNullOrEmpty(dr["DateOfBirth"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Race"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["MaritalStatus"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Gender"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["NoOfChildren"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Dialect"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["Religion"].ToString())))
                isValid = false;

            if (!isValid)
            {
                problemTabs.Add("Addition Personal Particulars");
                formIsComplete = false;
            }
        }


        //--Education Qualification
        //--Language Proficiency

        //--Professional Membership
        //--Awards
        //--Employment History


        //--Further Information
        if (dt.Rows.Count > 0)
        {
            isValid = true;
            dr = dt.Rows[0];

            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo1"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo2"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo3"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo4"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo5"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo6"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo7"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo8"].ToString())))
                isValid = false;
            if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo9"].ToString())))
                isValid = false;

            if (!isValid)
            {
                problemTabs.Add("Further Information");
                formIsComplete = false;
            }
        }
        //--Family Details
        //--Experience Living Abroad

        //--Interest & Hobbies
        //--Skills & Knowledge
        //--Applicant Photo
        dt = SRM.DataObjects.SRM_Photo.GetPhoto(applicantKey.ToString());
        if ((dt.Rows.Count <= 0) && (RadTabStrip.Tabs[14].Visible))
        {
            isValid = false;

            if (!isValid)
            {
                problemTabs.Add("Applicant Photo");
                formIsComplete = false;
            }
        }


        if (!formIsComplete)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("<p style='color:red;font-weight:bold;'>System Message: Your application have not been submitted as it is incomplete.<br/>Please fill in the required fields in the following sections: </p>");
            sb.AppendLine("<ol style='color:red;'>");
            foreach (string tabName in problemTabs)
            {
                sb.AppendLine(string.Format("<li>{0}", tabName));
            }

            sb.AppendLine("</ol>");

            this.errorMessage.Text = sb.ToString();
        }
        else
        {
            // Set Form as Complete
            Response.Redirect("~/SuccessfulSubmission.aspx");
        }

    } // protected void SubmitApplicationForm_Button_Click(object sender, EventArgs e)

</script>


<%@ Register src="UserControls/PersonalParticularsUserControl.ascx" tagname="PersonalParticularsUserControl" tagprefix="uc1" %>

<%@ Register src="UserControls/ResumeFileUserControl.ascx" tagname="ResumeFileUserControl" tagprefix="uc2" %>

<%@ Register src="UserControls/FamilyDetailsUserControl.ascx" tagname="FamilyDetailsUserControl" tagprefix="uc3" %>

<%@ Register src="UserControls/LanguageProficiencyUserControl.ascx" tagname="LanguageProficiencyUserControl" tagprefix="uc4" %>

<%@ Register src="UserControls/ProfessionalMembershipUserControl.ascx" tagname="ProfessionalMembershipUserControl" tagprefix="uc5" %>

<%@ Register src="UserControls/AwardsUserControl.ascx" tagname="AwardsUserControl" tagprefix="uc6" %>

<%@ Register src="UserControls/EmploymentHistoryUserControl.ascx" tagname="EmploymentHistoryUserControl" tagprefix="uc7" %>

<%@ Register src="UserControls/FurtherInformationUserControl.ascx" tagname="FurtherInformationUserControl" tagprefix="uc8" %>

<%@ Register src="UserControls/NationalServiceUserControl.ascx" tagname="NationalServiceUserControl" tagprefix="uc9" %>

<%@ Register src="UserControls/AdditionalPersonalParticularsUserControl.ascx" tagname="AdditionalPersonalParticularsUserControl" tagprefix="uc10" %>

<%@ Register src="UserControls/ApplicantPhotoUserControl.ascx" tagname="ApplicantPhotoUserControl" tagprefix="uc11" %>

<%@ Register src="UserControls/SkillSetUserControl.ascx" tagname="SkillSetUserControl" tagprefix="uc12" %>

<%@ Register src="UserControls/RecreationUserControl.ascx" tagname="RecreationUserControl" tagprefix="uc13" %>

<%@ Register src="UserControls/OverseasUserControl.ascx" tagname="OverseasUserControl" tagprefix="uc14" %>

<%@ Register src="UserControls/EducationQualification_UserControl.ascx" tagname="EducationQualification_UserControl" tagprefix="uc15" %>

<asp:Content ID="head_Content" ContentPlaceHolderID="head" Runat="Server"></asp:Content>
<asp:Content ID="main_Content" ContentPlaceHolderID="main" Runat="Server">
    <asp:HiddenField runat="server" ID="hdn_UseSAPCode" />
    <asp:HiddenField runat="server" ID="hdn_applicantKey" />
    <asp:HiddenField runat="server" ID="hdn_requisitionKey" />

    
    <asp:Literal runat="server" ID="role_literal"></asp:Literal>

    <p>
        Fill up your particulars in the form below.<br />
        To change your account password, click <a href="ChangePasswordForm.aspx">here</a>.
    </p>
    <%
        if (SRM.Help.GetAppSetting("Simulate_Mode").Equals("On", StringComparison.InvariantCultureIgnoreCase))
        {
        %>
        <p>
            <%= PreviousApplicant %>
            
            <%= Session["ApplicantKey"].ToString() %>

            <%= NextApplicant %>
        </p>
        <%
        }
        %>
    <asp:Literal runat="server" ID="errorMessage"></asp:Literal>
    
    
    <telerik:RadTabStrip ID="RadTabStrip" runat="server" SelectedIndex="0" 
        MultiPageID="RadMultiPage" Skin="Outlook" CausesValidation="False">
        <Tabs>
            <telerik:RadTab runat="server" Text="Personal Particulars" Owner="RadTabStrip" Selected="True" PageViewID="PersonalParticularsPageView"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Resume File" Owner="RadTabStrip" PageViewID="ResumeFilePageView"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Education Qualification" Owner="RadTabStrip" PageViewID="EducationQualificationPageView"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Language Proficiency" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Professional Membership" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Awards" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Employment History" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="National Service" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Further Information" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Addition Personal Particulars" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Family Details" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Experience Living Abroad" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Interest & Hobbies" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Skills & Knowledge" Owner="RadTabStrip"></telerik:RadTab>
            <telerik:RadTab runat="server" Text="Applicant Photo" Owner="RadTabStrip"></telerik:RadTab>
        </Tabs>
    </telerik:RadTabStrip>
    <hr />
    <telerik:RadMultiPage ID="RadMultiPage" runat="server" SelectedIndex="0">
        <telerik:RadPageView ID="PersonalParticularsPageView"  runat="server">
            <uc1:PersonalParticularsUserControl ID="PersonalParticularsUserControl1" runat="server" />
            </telerik:RadPageView>
        <telerik:RadPageView ID="ResumeFilePageView" runat="server">
            <uc2:ResumeFileUserControl ID="ResumeFileUserControl1" runat="server" />
            </telerik:RadPageView>
        <telerik:RadPageView ID="EducationQualificationPageView" runat="server">
            <uc15:EducationQualification_UserControl ID="EducationQualification_UserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="LanguageProficiencyPageView" runat="server">
            <uc4:LanguageProficiencyUserControl ID="LanguageProficiencyUserControl1" runat="server" />
            </telerik:RadPageView>
        <telerik:RadPageView ID="ProfessionalMembershipPageView" runat="server">
            <uc5:ProfessionalMembershipUserControl ID="ProfessionalMembershipUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="AwardsPageView" runat="server">
            <uc6:AwardsUserControl ID="AwardsUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="EmploymentHistoryPageView" runat="server">
            <uc7:EmploymentHistoryUserControl ID="EmploymentHistoryUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="NationalServicePageView" runat="server">
            <uc9:NationalServiceUserControl ID="NationalServiceUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="FurtherInformatinoPageView" runat="server"><uc8:FurtherInformationUserControl 
                ID="FurtherInformationUserControl1" runat="server" />
                </telerik:RadPageView>
        <telerik:RadPageView ID="AdditionPersonalParticularsPageView" runat="server">
            <uc10:AdditionalPersonalParticularsUserControl ID="AdditionalPersonalParticularsUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="FamilyDetailsPageView" runat="server">
            <uc3:FamilyDetailsUserControl ID="FamilyDetailsUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="ExperienceLivingAbroadPageView" runat="server">
            <uc14:OverseasUserControl ID="OverseasUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="InterestAndHobbiesPageView" runat="server">
            <uc13:RecreationUserControl ID="RecreationUserControl1" runat="server" />
        </telerik:RadPageView>
        <telerik:RadPageView ID="SkillsAndKnowledgePageView" runat="server">
            <uc12:SkillSetUserControl ID="SkillSetUserControl1" runat="server" />
        </telerik:RadPageView>

        <telerik:RadPageView ID="ApplicantPhotoRadPageView" runat="server">
            <uc11:ApplicantPhotoUserControl ID="ApplicantPhotoUserControl1" runat="server" />
        </telerik:RadPageView>

        
    </telerik:RadMultiPage>

    <div style="border-top: 4px solid #000;margin-top:1em;padding:1em 0;">
    <asp:Button ID="SaveApplicationForm_Button" runat="server"  
            onclick="SaveApplicationForm_Button_Click" Text="Save Application Form" />
        <asp:Button ID="SubmitApplicationForm_Button" runat="server"  
            onclick="SubmitApplicationForm_Button_Click" Text="Submit Application Form" />
    </div>
</asp:Content>

